# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
import sys


class Solution:
    mini = -sys.maxsize

    # 求一棵二叉树中 和最大的路径 可以不经过根节点
    # 用递归 每次取左右两者的最大值 但是只返回某一条路径 因为这样还可以与父节点组合
    def maxPathSum(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.help(root)
        return self.mini

    def help(self, root):
        if root is None:
            return 0
        left = max(0, self.help(root.left))
        right = max(0, self.help(root.right))
        self.mini = max(self.mini, root.val + left + right)
        return root.val + max(left, right)
